package com.t18;

import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * Created by xuyiwei on 2018/9/2.
 */
public class UsingRandomAccessFile {
    static String file = "/Users/xuyiwei/Documents/javaBase/jdk/src/com/t18/UsingRandomAccessFile.txt";
    static void display() throws IOException {
        RandomAccessFile rf = new RandomAccessFile(file,"r");
        for(int i=0;i<7;i++){
            System.out.println("Value "+i+":"+rf.readDouble());
        }
        System.out.println(rf.readUTF());
        rf.close();
    }

    public static void main(String[] args) throws IOException {
        RandomAccessFile rf = new RandomAccessFile(file,"rw");
        for(int i=0;i<7;i++){
            rf.writeDouble(i*0.77);
        }
        rf.writeUTF("Then end of the file");
        rf.close();
        display();
        rf = new RandomAccessFile(file,"rw");
        rf.seek(5*8);
        rf.writeDouble(47.0001);
        rf.close();
        display();
        /**
         * Value 0:0.0
         Value 1:0.77
         Value 2:1.54
         Value 3:2.31
         Value 4:3.08
         Value 5:3.85
         Value 6:4.62
         Then end of the file
         Value 0:0.0
         Value 1:0.77
         Value 2:1.54
         Value 3:2.31
         Value 4:3.08
         Value 5:47.0001
         Value 6:4.62
         Then end of the file
         */
    }
}
